package actions;

import game.constants.ReferenceAction;
import game.constants.ReferenceSpecialFeature;
import interfaces.ActionInterface;
import interfaces.impl.Action;
import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import constants.Constants;
import constants.Message;
import extra.Position;

public class ActionUniverseTransport extends Action{
	
	private int universeIdToTransport=constants.Constants.NO_UNIVERSE_ID;
	private Position position=null;

	@Override
	public ActionInterface cloneAction() {
		ActionUniverseTransport action = new ActionUniverseTransport();
		action.modifyAction(this);
		return action;
	}

	@Override
	public boolean processActionChild(Universe universe) {
		
		if(universeIdToTransport==Constants.NO_UNIVERSE_ID){
			return false;
		}
		
		
		
		Universe universeToMove = universe.getSpace().getUniverse(universeIdToTransport);
		if(universeToMove==null){
			return false;
		}
		
		universe.removeObject(target);
		
		int x=100;
		int y=100;
		
		if(position!=null){
			x=(int)position.getX();
			y=(int)position.getY();
		}
		target.setPosition(new Position(x,y));
		
		
		
		WorldObject obj = new WorldObject(target);
		
		obj.clearImmediatlyActions();
		
		SpetialFeature xtomove = obj.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_X_TO_MOVE);
		SpetialFeature ytomove = obj.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_Y_TO_MOVE);
		xtomove.setValue(x);
		ytomove.setValue(y);
		
		
		universeToMove.addObject(obj);
		
		
		String message = new String();
		message = Message.CLIENT_UNIVERSE_TRANSPORT+System.getProperty("line.separator")+universeIdToTransport;
		
		universe.generateMessage(target.getOwner(), message);
		message =Message.POSITION_CAMERA+System.getProperty("line.separator")+obj.getPosition().getX()+System.getProperty("line.separator")+obj.getPosition().getY();
		universe.generateMessage(target.getOwner(), message);
		
		
			
		return true;
	}

	@Override
	public void initializeName() {
		name="Action Universe Transport";
		
	}

	@Override
	public void initializeId() {
		
		id=ReferenceAction.ACTION_UNIVERSE_TRANSPORT;
		
	}

	@Override
	public void modifyChild(ActionInterface action) {
		if (action instanceof ActionUniverseTransport){
			universeIdToTransport=((ActionUniverseTransport) action).universeIdToTransport;
			position=((ActionUniverseTransport) action).position;
		}
		
	}

	public int getUniverseIdToTransport() {
		return universeIdToTransport;
	}

	public void setUniverseIdToTransport(int universeIdToTransport) {
		this.universeIdToTransport = universeIdToTransport;
		
	}

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}
	
	
	
	

}
